﻿@{
    ViewData["Title"] = "Index";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml";
}
@section head{
    <script type="text/javascript">
        var renderTable;
        var userType = 0;
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            treetable: 'treetable-lay/treetable',
            index: 'lib/index'
        }).use(['table', 'form', 'treetable', 'index'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , table = layui.table
                , element = layui.element
                , treetable = layui.treetable;
            element.render();
            element.on('tab(component-tabs-brief)', function (obj) {
                userType = obj.index;
                renderTable();
            });
            // 渲染表格
            renderTable = function () {
                layer.load(2);
                treetable.render({
                    elem: '#table-datalist'
                    , treeColIndex: 1
                    , treeSpid: 0
                    , treeIdName: 'id'
                    , treePidName: 'pid'
                    , url: '/Console/Menus/GetListJson?userType=' + userType
                    , toolbar: '#table-datalist-toolbar'
                    , limit: 20
                    , title: '菜单数据表'
                    , cols: [[
                        { type: 'checkbox', fixed: 'left' }
                        , { field: 'names', title: '名称', width: 200, style: 'text-align:left'}
                        , { field: 'url', title: '链接地址', width: 250, style: 'text-align:left' }
                        , { field: 'remark', title: '备注', style: 'text-align:left' }
                        , { field: 'creator', title: '创建人', width: 150 }
                        , { field: 'createDate', title: '添加时间', width: 200 }
                        , { field: 'sort', title: '排序', width: 100 }
                        , {
                            field: 'state', title: '状态', width: 110, templet: function (res) {
                                if (res.state == 1)
                                    return '<input type="checkbox" name="state" lay-skin="switch" lay-text="On|Off" lay-filter="table-state" value="' + res.id + '" checked="checked">'
                                else
                                    return '<input type="checkbox" name="state" lay-skin="switch" lay-text="On|Off" lay-filter="table-state" value="' + res.id + '">'
                            }
                        }
                        , { field: 'operate', fixed: 'right', title: '操作', toolbar: '#table-datalist-bar', width: 220 }
                    ]]
                    , page: { layout: ['prev', 'page', 'next', 'refresh'] }
                    , done: function (res) {
                        if (res.count == 0) {
                            $(".layui-table-main").html('<div class="layui-none">' + top.lang.noData + '</div>');
                        }
                        layer.closeAll('loading');

                        /**多语言初始化 */
                        top.SetListPageLanguage(layui);
                    }
                });
            };
            //加载表格
            renderTable();

            //头工具栏事件
            table.on('toolbar(table-datalist)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                switch (obj.event) {
                    case 'addNew':
                        top.OpenPageByKey(layui, "addNewMenus", 2, '800px', '650px', '/Console/Menus/Create?pid=0&userType=' + userType);
                        break;
                    case 'DelItems':
                        var data = checkStatus.data;
                        if (data.length > 0) {
                            var ids = "";
                            for (var i = 0; i < data.length; i++) {
                                if (ids == "")
                                    ids = data[i].id;
                                else
                                    ids += "," + data[i].id;
                            }
                            if (ids != "") {
                                var postData = {
                                    ids: ids
                                };
                                top.ConfirmByKey(layui, 'confirmDelete', postData, "/Console/Menus/DeleteItems", "POST", "deleteSuccess", "deleteFail", function () {
                                    renderTable();
                                });
                            }
                        }
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(table-datalist)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    var postData = {
                        id: data.id
                    };
                    top.ConfirmByKey(layui, 'confirmDelete', postData, "/Console/Menus/Delete", "POST", "deleteSuccess", "deleteFail", function () {
                        renderTable();
                    });
                } else if (obj.event === 'edit') {
                    top.OpenPageByKey(layui, "editMenus", 2, '800px', '650px', '/Console/Menus/Edit?id=' + data.id + '&userType=' + userType);
                } else if (obj.event === 'lower') {
                    top.OpenPageByKey(layui, "rightsManagement", 2, '1000px', '650px', '/Console/Power/Index?menuid=' + data.id + '&userType=' + userType);
                }
                else if (obj.event === 'add') {
                    top.OpenPageByKey(layui, "addNewMenus", 2, '800px', '650px', '/Console/Menus/Create?pid=' + data.id + '&userType=' + userType);
                }
            });

            //监听锁定操作
            form.on('switch(table-state)', function (obj) {
                var state = 1;
                if (obj.elem.checked) {
                    state = 1;
                } else {
                    state = 0;
                }
                $.ajax({
                    type: "POST",
                    url: "/Console/Menus/UpdateState",
                    data: {
                        id: this.value
                        , intState: state
                    }, // 你的formid
                    async: true,
                    error: function (request) {
                        alert("Connection error");
                    },
                    beforeSend: function (xhr) {
                        layui.layer.load();
                    },
                    success: function (data) {
                        var item = eval(data);
                        if (item.code == 0) {
                            top.ShowMessageByKey(layui, "submitSuccess", 6, 1000, function () {
                                renderTable();
                            });
                        } else {
                            /**提示语弹框 */
                            top.AlertByKey(layui, "submitFail", 0);
                        }
                        layer.closeAll('loading');
                    }
                });
            });

        });
    </script>
}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-tab layui-tab-brief" lay-filter="component-tabs-brief">
                        <ul class="layui-tab-title">
                            <li lay-event="manageMenus" class="layui-this">管理员</li>
                            <li lay-event="userMenus">用户</li>
                        </ul>
                        <div class="layui-tab-content">
                            <table class="layui-hide" id="table-datalist" lay-filter="table-datalist"></table>
                            <script type="text/html" id="table-datalist-toolbar">
                                <div class="layui-btn-container" id="layui-btn-container">
                                    <button class="layui-btn layui-btn-sm" lay-event="addNew">新增</button>
                                    <button class="layui-btn layui-btn-sm" lay-event="delItems">批量删除</button>
                                </div>
                            </script>
                            <script type="text/html" id="table-datalist-bar">
                                <button class="layui-btn layui-btn-sm" lay-event="edit">编辑</button>
                                <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</button>
                                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add">添加下级</button>
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>